package net.pm.core.web.product;

import java.util.Date;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.Pager;
import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.dateEdtor.DateEditor_Long;
import net.pm.core.entities.db.Product;
import net.pm.core.service.ProductServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/product/")
public class ProductController extends BaseController{

	@Resource(name="DateEditor_Long")
	private DateEditor_Long dateEditor_Long;
	@Resource(name="ProductServicer")
	private ProductServicer productServicer;
	
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public ModelAndView manage(){
		ModelAndView mav = new ModelAndView("product/product_manage");
		return mav;
	}
	
	/**
	 * 默认页
	 * @return
	 */
	@RequestMapping(value="page",method=RequestMethod.GET)
	public ModelAndView page() {
		return page(null, null, null,
				DevConstant.PAGE_DEFAULT_SIZE, DevConstant.PAGE_DEFAULT_NO);
	}
	
	/**
	 * 分页查找
	 * @return
	 */
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ModelAndView page(String productName, Date startdate, Date enddate, Integer pagesize, Integer pageno) {
		ModelAndView mav = new ModelAndView("product/product_page");
		Pager<Product> pager = productServicer.findProductPager(
				productName, startdate, enddate, pagesize, pageno);

		mav.addObject("pager", pager);
		return mav;
	}
	
	/**
	 * 查看产品详细信息
	 * @param productId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.VIEW_PRODUCT)
	@RequestMapping(value="info",method=RequestMethod.GET)
	public ModelAndView info(Long productId){
		ModelAndView mav = new ModelAndView("product/product_info");
		Product product = productServicer.findEntityById(productId);
		mav.addObject("product", product);
		
		return mav;
	}
	
	
	/**
	 * 数据绑定
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, dateEditor_Long);
	}
	
	
}
